<?php
include_once("../configs/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
require_once("controlHeader.php");					//--> system control header
include_once("InfoClass.php");						//--> InfoClass
include_once("Files.php");							//--> Files
include_once("PicUpload.php");						//--> PicUpload
include_once("Pic.php");							//--> Pic
require_once("../tools/fckeditor/fckeditor.php") ;

$objOperate 	= new Operation($sessionImpl->session['lan']);			//--> Operation
$objInfoClass	= new InfoClass($sessionImpl->session['lan'], $uid);		//--> InfoClass
$objPicUpload	= new PicUpload($sessionImpl->session['lan'], $uid);		//--> PicUpload
$objPic			= new Pic($sessionImpl->session['lan'], $uid);			//--> Audio
$objFiles		= new Files();											//--> Files

$error_message 	= '';
if(isset($_GET['picid']))
	$picid	= $_GET['picid'];
else if(isset($_POST['picid']))
	$picid	= $_POST['picid'];
	
$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(isset($_POST['Submit'])){
	$picid	= $_POST['picid'];
	$formatid	= $_POST['formatid'];

	if(isset($_FILES['upload']['name'])){
		$file_name 	= $_FILES['upload']['name'];
		$file_type 	= $_FILES['upload']['type'];
		$file_tmp  	= $_FILES['upload']['tmp_name'];
		$file_size 	= $_FILES['upload']['size'];
	}else
		$file_name 	= "";
	
	if(empty($picid))
		$error_message = 'picid id should\'t be empty.';
	else if(empty($formatid))
		$error_message = 'file format id should\'t be empty.';
	else if(empty($file_name))
		$error_message = 'please upload a file.';
	else if($objPicUpload->isExisted($arrOperate, $picid, $formatid))
		$error_message = 'this format file is existed.';
	else{
		if(!empty($file_name)){
			$arrDataInfo = $objInfoClass->getFromID($arrOperate, $formatid);
			if(is_array($arrDataInfo)){
				$fileName = $picid.'.'.$arrDataInfo['name'];
				
				/*-- get ext file name --*/
				$arr_extend_name = explode('.',$file_name);
				$extend_name = $arr_extend_name[count($arr_extend_name)-1];
				if(strtolower($extend_name) == $arrDataInfo['name']){
					$file_path = $objFiles->strGetFullPathByID(PIC_DOWNLOAD_DIR, $picid, $fileName);
					
					if (!copy($file_tmp, $file_path)) {//-- save file
						$error_message = "upload file failure.";
					}
				}else{
					$error_message = "It isn't defference between upload file format and select format.";
				}
			}else{
				$error_message = "can't find this format from format id for ".$formatid;
			}
		}
		
		if(empty($error_message)){
			$isReturn = $objPicUpload->add($arrOperate, $picid, $formatid, $file_size);
			if($isReturn){
				$objPic->editStatus($arrOperate, $picid, "wait");
				
				$error_message = 'add successfully.';
			}else 
				$error_message = 'add failure.';
		}
	}
}

$arrFileFormat = $objInfoClass->getDir($arrOperate, 56);

/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_DIR;
$smarty->compile_dir  = COMPILE_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrFileFormat', $arrFileFormat);
$smarty->assign('picid', $picid);
$smarty->assign('formatidS', $formatid);

$smarty->display('listPicUpload_Add.htm');
?>
